from flask import Blueprint, request, jsonify
import random
from db import get_data, db1
from utils.redisconn import r

bp_auser = Blueprint('adminuser', __name__)
from utils.comm import send_message


@bp_auser.route('/sendsms')
def sendsms():
    mobile = request.args.get('mobile')
    code = random.randint(1000, 9999)
    flag = send_message(mobile, code, 1)
    if flag:
        r.str_set(mobile, code, 30)
    else:
        pass

    return jsonify({"code": 200})


@bp_auser.route('/login')
def login():
    data = get_data()
    value = r.str_get(data['mobile'])
    if not value:
        return jsonify({'code': 10011, 'mes': '验证码已过期，请重新发送'})
    if int(value) == int(data['code']):
        sql = "select * from admin_user where name='%s'" % (data['mobile'])
        res = db1.find(sql)
        return jsonify({'code': 200, 'user': res})
    else:
        return jsonify({'code': 10012, 'mes': '验证码输入错误'})
